//
//  DisAndNutViewController.m
//  WE
//
//  Created by iMacOne on 15/11/24.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "DisAndNutViewController.h"
#import "Size.h"
#import "ToolUtil.h"
#import "AssessCell.h"
#import <Masonry.h>
#import "CourseControl.h"
@interface DisAndNutViewController (){
    int i;
}

@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)NSArray * titleArray;
@property (nonatomic, assign)BOOL isOpen;


@end

@implementation DisAndNutViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isOpen = YES;
    i = 102;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"健康评估";
    self.titleArray = @[@"请评估下自己的身材",@"每天看手机多长时间?",@"每天看电脑多长时间?",@"平均每天在地铁/公交车中看手机的时间?",@"您觉得自己实力在下降吗?",@"有没有觉得手指有发麻感?",@"最近有没有小腿抽筋?",@"",@""];
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView.backgroundColor = [UIColor yellowColor];
    [self setExtraCellLineHidden:self.tableView];
}
- (UIView *)creatImageView
{
    UIImageView * aImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 100)];
    aImageV.backgroundColor = DEBUG_CLOLR;
    aImageV.userInteractionEnabled = YES;
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(WindowWidth - 70, 25, 60, 50);
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    btn.titleLabel.font = FONT(12);
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 3;
    btn.layer.borderWidth = 1;
    btn.layer.borderColor = GRAYCOLOR.CGColor;
    btn.tag = 100;
    
    [btn setTitle:@"健康历程" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(doClick:) forControlEvents:UIControlEventTouchUpInside];

    [aImageV addSubview:btn];
    
    return aImageV;
    
}
- (UIView *)creatFooterView
{
    UIView * aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 160)];
//    aView.backgroundColor = [UIColor greenColor];
    
    UILabel * lbl1 = [UILabel new];
    [aView addSubview:lbl1];
    //    lbl1.backgroundColor = DEBUG_CLOLR;
    lbl1.text = @"积分规则:";
    lbl1.font = FONT(14);
    //    lbl1.textColor = [UIColor grayColor];
    //    lbl1.textAlignment = NSTextAlignmentCenter;
    [lbl1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(aView).with.offset(60);
        make.left.equalTo(aView).with.offset(15);
        make.height.mas_equalTo(20);
        //        make.size.mas_equalTo(CGSizeMake(200, 20));
    }];
    
    
    UILabel * lbl2 = [UILabel new];
    [aView addSubview:lbl2];
    //    lbl2.backgroundColor = DEBUG_CLOLR;
    lbl2.text = @"1 累计计分,满分100";
//    lbl2.textColor = GRAYCOLOR;
    lbl2.font = FONT(12);
        lbl2.textColor = [UIColor grayColor];
    //    lbl1.textAlignment = NSTextAlignmentCenter;
    [lbl2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lbl1.mas_bottom).with.offset(5);
        make.left.equalTo(lbl1.mas_left);
        make.height.mas_equalTo(20);
        //        make.size.mas_equalTo(CGSizeMake(200, 20));
    }];
    
    UILabel * lbl3 = [UILabel new];
    [aView addSubview:lbl3];
    //    lbl3.backgroundColor = DEBUG_CLOLR;
    lbl3.text = @"2 单类得分率低于%40, 扣除总分20分";
    lbl3.font = FONT(12);
    lbl3.textColor = [UIColor grayColor];
    //    lbl1.textAlignment = NSTextAlignmentCenter;
    [lbl3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lbl2.mas_bottom).with.offset(5);
        make.left.equalTo(lbl1.mas_left);
        make.height.mas_equalTo(20);
        //        make.size.mas_equalTo(CGSizeMake(200, 20));
    }];
    
    UILabel * lbl4 = [UILabel new];
    [aView addSubview:lbl4];
    //    lbl4.backgroundColor = DEBUG_CLOLR;
    lbl4.text = @"3 凭身份证和短信去医院就诊";
    lbl4.numberOfLines = 0;
    lbl4.font = FONT(12);
//    lbl4.textColor = GRAYCOLOR;
    lbl4.textColor = [UIColor grayColor];
    lbl1.textAlignment = NSTextAlignmentLeft;
    [lbl4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lbl3.mas_bottom).with.offset(5);
        make.left.equalTo(lbl1.mas_left);
        
        //        make.size.width.offset(200);
        make.height.mas_equalTo(20);
        //        make.centerY.equalTo(self.view);
        //        make.size.mas_equalTo(CGSizeMake(200, 0));
    }];
    
    UIButton * btn = [UIButton new];
    [aView addSubview:btn];
    
    btn.backgroundColor = [UIColor blueColor];
    [btn setTitle:@"提交" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(doClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = FONT(14);
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lbl4.mas_bottom).offset(20);
        make.left.equalTo(lbl1.mas_left).offset(40);
        make.centerX.equalTo(aView);
        //        make.size.mas_equalTo(CGSizeMake(200, 40));
        make.height.offset(40);
    }];
    
    return aView;

}
- (void)doClick:(UIButton *)btn
{
    if (btn.tag == 100) {
        [self.navigationController pushViewController:[[CourseControl alloc] init] animated:YES];
    }
}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,WindowWidth , WindowHeight ) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
//        _tableView.tableFooterView = [self creatFooterView];

    }
    return _tableView;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    if (section == 0) {
        return [self creatImageView];
    }else if (section == 1){
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 40)];
        UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 60, 20)];
        lbl.text = @"测试题目";
        lbl.font = FONT(14);
        UILabel * lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lbl.frame) + 5, 10, 50, 20)];
        lbl1.text = @"单选";
        lbl1.font = FONT(12);
        lbl1.textColor = [UIColor grayColor];
        [view addSubview:lbl1];
        [view addSubview:lbl];
        [ToolUtil createLine:39 superView:view lift:0 width:WindowWidth hight:1 color:GRAYCOLOR];
        return view;
    }else if (section == 7) {
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 40)];
        view.backgroundColor = [UIColor whiteColor];
        return view;
    }
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, WindowWidth, 40);
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    btn.titleLabel.font = FONT(12);
    
    //    [btn setTitle:@"健康历程" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    UILabel * contentLbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, [ToolUtil autoWidth:self.titleArray[section] width:0 height:20 num:14], 20)];
    contentLbl.font = FONT(14);
    contentLbl.text = self.titleArray[section];
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = section + 102;
    [ToolUtil createLine:39 superView:btn lift:0 width:WindowWidth hight:1 color:GRAYCOLOR];
    [btn addSubview:contentLbl];
    return btn;
    
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 8) {
        return [self creatFooterView];
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 8) {
        return 240;
    }
    return 0;
}
- (void)clickBtn:(UIButton *)btn
{
    i = (int)btn.tag - 2;
    if (self.isOpen) {
        self.isOpen = NO;
    }else{
        self.isOpen = YES;
    }
//    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:3] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView reloadData];
//    [self.tableView reloadData];

}
- (void)doClickBtn:(NSInteger)number cell:(UITableViewCell *)cell
{
    NSLog(@"%ld",number);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 100;
    }
    return 40;
    
}
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (i-100 == section) {
        if (self.isOpen == NO) {
            
            return 1;
        }

    }
        return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString * cellIdentifier = @"AssessCell";
    AssessCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[AssessCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//        cell.backgroundColor = DEBUG_CLOLR;
        cell.delegate = self;
    }
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 9;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *coverView = [UIView new];
    tableView.tableFooterView = coverView;
}
@end
